package com.boil.qz.safekavass.web.controller;

import com.boil.pojo.ResultVo;
import com.boil.qz.safekavass.pojo.ReportCountByReceiveredpackMonthVo;
import com.boil.qz.safekavass.pojo.ReportCountByReportMonthVo;
import com.boil.qz.safekavass.pojo.ReportCountBySendredpackMonthVo;
import com.boil.qz.safekavass.service.ReportResService;
import com.boil.qz.safekavass.web.request.param.MonthParam;
import com.boil.util.Constants;
import com.boil.util.DateUtils;
import com.boil.util.OkBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ChenYong on 2017-05-23.
 * <br>
 * 举报统计 Controller。
 */
@Controller
@RequestMapping("/web/report")
public class ReportCountController extends OkBaseController {
    /**
     * 注入举报 Service
     */
    @Autowired
    private ReportResService reportResService;

    /**
     * 举报统计页面。
     *
     * @param model 模型
     * @return 举报统计页面
     */
    @RequestMapping(method = RequestMethod.GET, value = "/count.html")
    public String countHtml(Model model) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 当前时间
        Date currTime = new Date();
        // 年份集合
        List<Integer> years = new ArrayList<Integer>();
        // 开始年份
        int startYear = Constants.VALUE_2017;
        // 结束年份
        int endYear = Integer.valueOf(DateUtils.format(currTime, Constants.DATE_FORMAT_4));
        // 当前月份
        int currMonth = Integer.valueOf(DateUtils.format(currTime, Constants.DATE_FORMAT_5));

        if (startYear == endYear) {
            years.add(startYear);
        } else {
            for (int i = startYear; i <= endYear; i++) {
                years.add(i);
            }
        }

        resultVo.getResults().add(years);
        resultVo.getResults().add(currMonth);

        model.addAttribute(Constants.RESULT_VO, resultVo);

        return "/report/count";
    }

    /**
     * 根据举报月份统计举报情况。
     *
     * @param model  模型
     * @param param  月份请求参数
     * @param errors 请求参数的校验结果
     * @return 根据举报月份统计举报情况 Json
     */
    @RequestMapping(method = RequestMethod.GET, value = "/count.json")
    @ResponseBody
    public ResultVo countJson(Model model, @Valid MonthParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 根据举报月份统计举报情况
        List<ReportCountByReportMonthVo> reportCountByReportMonthVos = reportResService.findCountByReportMonth(param.getMonth());

        resultVo.getResults().add(DateUtils.format(DateUtils.parse(param.getMonth(), Constants.DATE_FORMAT_6), Constants.DATE_FORMAT_2));
        resultVo.getResults().add(reportCountByReportMonthVos);

        return resultVo;
    }

    /**
     * 根据发红包月份统计红包发放情况。
     *
     * @param model  模型
     * @param param  月份请求参数
     * @param errors 请求参数的校验结果
     * @return 根据发红包月份统计红包发放情况 Json
     */
    @RequestMapping(method = RequestMethod.GET, value = "/sendredpack/count.json")
    @ResponseBody
    public ResultVo sendredpackCountJson(Model model, @Valid MonthParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 根据发红包月份统计红包发放情况
        List<ReportCountBySendredpackMonthVo> reportCountByReportMonthVos = reportResService.findCountBySendredpackMonth(param.getMonth());

        resultVo.getResults().add(DateUtils.format(DateUtils.parse(param.getMonth(), Constants.DATE_FORMAT_6), Constants.DATE_FORMAT_2));
        resultVo.getResults().add(reportCountByReportMonthVos);

        return resultVo;
    }

    /**
     * 根据领红包月份统计红包领取情况。
     *
     * @param model  模型
     * @param param  月份请求参数
     * @param errors 请求参数的校验结果
     * @return 根据领红包月份统计红包领取情况 Json
     */
    @RequestMapping(method = RequestMethod.GET, value = "/receiveredpack/count.json")
    @ResponseBody
    public ResultVo receiveredpackCountJson(Model model, @Valid MonthParam param, BindingResult errors) {
        // 结果 Vo
        ResultVo resultVo = new ResultVo();
        // 根据领红包月份统计红包领取情况
        List<ReportCountByReceiveredpackMonthVo> reportCountByReportMonthVos = reportResService.findCountByReceiveredpackMonth(param.getMonth());

        resultVo.getResults().add(DateUtils.format(DateUtils.parse(param.getMonth(), Constants.DATE_FORMAT_6), Constants.DATE_FORMAT_2));
        resultVo.getResults().add(reportCountByReportMonthVos);

        return resultVo;
    }
}